package com.zzuli.yxy.handler;

import com.alibaba.fastjson.JSON;
import com.zzuli.yxy.enumeration.AppHttpCodeEnum;
import com.zzuli.yxy.result.R;
import com.zzuli.yxy.util.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证失败处理器
 * @auther pony
 * @create 2023-03-13 21:49
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        R result = R.success(AppHttpCodeEnum.AUTHENTICATION_FAILED);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(httpServletResponse, json);
    }
}
